#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################

rm(list=ls())

source("C:/UQAR/Recherche/Maitrise/Donnees/R/DataManagement/loadData.R")


MOBILE_WORKING_DIR <- "C:/UQAR/Recherche/Maitrise/Donnees/Bylot/Mobile"
MOVEBANK_MOBILE_PATH <- "C:/UQAR/Recherche/Maitrise/Donnees/Movebank/Filtered/Mobile/Argos_mobile2.csv"
MOVEBANK_MOBILE_PATH2 <- "C:/UQAR/Recherche/Maitrise/Donnees/Movebank/Filtered/Mobile/Argos_mobile3.csv"

locsclasses=c(3, 2, 1, 0, "A", "B")

#'#
#'# Recuperation des donnees statiques et comparaison avec le gps
#'#

mobile.errors.raw <- loadData(MOBILE_WORKING_DIR, static=FALSE)
mobile.errors.mb <- loadMovebankData(MOVEBANK_MOBILE_PATH)
mobile.errors.mb2 <- loadMovebankData(MOVEBANK_MOBILE_PATH2)

#'#################################################################################################
#'#                                                                                              ##
#'#                                 Proportion de localisations                                  ##
#'#                                                                                              ##
#'#################################################################################################




res1 <- compare.filters.desc.mobile(mobile.errors.raw, mobile.errors.mb)
res2 <- compare.filters.desc.mobile(mobile.errors.raw, mobile.errors.mb2)



#'#
#'# TODO : faire des graphs qui ont de la gueule
#'# 
cols <- c("mistyrose2", "indianred3", "darkred")
ylim <- c(0, 85)


mobile.freqs.raw <- static.freqs(mobile.errors.raw)
mobile.freqs.mb <- static.freqs(mobile.errors.mb)
mobile.freqs.mb2 <- static.freqs(mobile.errors.mb2)

freq.raw <- mobile.barchart(mobile.freqs.raw, col=cols, ylimits=ylim,
		main=paste("Donnees brutes, nlocs:", nrow(mobile.errors.raw)))
freq.mb <- mobile.barchart(mobile.freqs.mb, col=cols, ylimits=ylim,
		main=paste("Movebank (LC3, maxredun: 0.5, minrate: 0.5), nlocs:", nrow(mobile.errors.mb)))
freq.mb2 <- mobile.barchart(mobile.freqs.mb2, col=cols, ylimits=ylim,
		main=paste("Movebank (all, maxredun: 0.5, minrate: 0.5), nlocs:", nrow(mobile.errors.mb2)))

print.multi(list(freq.raw, freq.mb, freq.mb3, freq.mb2), width=500, height=500, save=TRUE,
		save.dest="C:/UQAR/Recherche/Maitrise/Donnees/Resultats/compareStaticFreqs.png")




#'#################################################################################################
#'#                                                                                              ##
#'#                                 Evaluation de l'erreur                                       ##
#'#                                                                                              ##
#'#################################################################################################


#'#
#'# Distributions cumulatives
#'#

cols <- grey(c(.9, .7, .5, .3))

locs.props.raw <- locs.error.prop(mobile.errors.raw)
locs.props.mb <- locs.error.prop(mobile.errors.mb)
locs.props.mb2 <- locs.error.prop(mobile.errors.mb2)

g1 <- barchart(locs.props.raw, stack=FALSE, horizontal=FALSE, col=cols, key=list(text=list(colnames(locs.props.raw)), 
				rectangles=list(col=cols), x=0, y=0.98, corner=c(0, 1)),
		ylim=c(0,90),
		xlab="Classes de localisation",
		ylab="Proportion de localisations (%)",
		main=paste("Donnees brutes, nlocs: ", nrow(mobile.errors.raw)))
g2 <- barchart(locs.props.mb, stack=FALSE, horizontal=FALSE, col=cols, key=list(text=list(colnames(locs.props.raw)), 
				rectangles=list(col=cols), x=0, y=0.98, corner=c(0, 1)),
		ylim=c(0,90),
		xlab="Classes de localisation",
		ylab="Proportion de localisations (%)",
		main=paste("All, maxred: 2, minrate: 8, nlocs: ", nrow(mobile.errors.mb)))
g3 <- barchart(locs.props.mb2, stack=FALSE, horizontal=FALSE, col=cols, key=list(text=list(colnames(locs.props.raw)), 
				rectangles=list(col=cols), x=0, y=0.98, corner=c(0, 1)),
		ylim=c(0,90),
		xlab="Classes de localisation",
		ylab="Proportion de localisations (%)",
		main=paste("All, maxred: 1, minrate: 8, nlocs: ", nrow(mobile.errors.mb2)))

print.multi(list(g1, g2, g3), width=500, height=500, ncol=1, nrow=1,
		save=TRUE, save.dest="C:/UQAR/Recherche/Maitrise/Donnees/Resultats/compareMobileLocRep2.png")




#'#################################################################################################
#'#                                                                                              ##
#'#                                 Gains et pertes                                       ##
#'#                                                                                              ##
#'#################################################################################################

cols <- c("mistyrose2", "indianred3", "darkred")

res1 <- compare.filters.desc.mobile(mobile.errors.raw, mobile.errors.mb)
res2 <- compare.filters.desc.mobile(mobile.errors.raw, mobile.errors.mb2)

props <- rbindlist(list(res1[, list(mean=mean(proplocs), sd=sd(proplocs), filter="mb1"), by=LC ],
				res2[, list(mean=mean(proplocs), sd=sd(proplocs), filter="mb2"), by=LC ]))


p <- barchart(mean ~ LC, props, groups=as.factor(filter), stack=FALSE,
		ly=props[, mean - sd],
		uy=props[, mean + sd], prepanel=prepanel.errorbar, panel=panel.errorbar,
		col=cols, box.ratio=3, origin=0, ylim=c(0,90),
		key=list(text=list(c("LC3, maxredun : 0.5, minrate:0.5",
								"All, maxred: 2, minrate: 8",
								"All, maxred: 1, minrate: 8")), 
				rectangles=list(col=cols), x=1, y=0.98, corner=c(1, 1)),
		xlab="Classes de localisation",
		ylab="Proportion de localisations supprim\u00e9es (%)",
		main="Proportion de localisations supprim\u00e9es en fonction par filtre",
		scales = list(col=1,tck=c(1,0)),
)

print.multi(list(p), 
		save=TRUE, save.dest="C:/UQAR/Recherche/Maitrise/Donnees/Resultats/pertesMobile.png")

